//
// Created by yangchao on 2022/4/18.
// 1. 两数之和: https://leetcode-cn.com/problems/two-sum/
//

#include <vector>
#include <map>
#include <unordered_map>

using namespace std;

class TwoSum {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int len = nums.size();
        for (int i = 0; i < len; ++i)
            for (int j = i + 1; j < len; ++j) {
                if (nums[i] + nums[j] == target) {
                    return {i, j};
                }
            };
        return {};
    }

    //哈希表法:推荐
    vector<int> twoSum2(vector<int>& nums, int target) {
        unordered_map<int,int> hashmap;
        for (int i = 0; i < nums.size(); ++i) {
            auto it =  hashmap.find(target-nums[i]);
            if (it != hashmap.end()) {
                return {it->second,i};
            }
            hashmap[nums[i]] = i;
        }
        return {};
    }
};

